/*
 * Copyright (C) 2010 Fred Barrie This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received
 * a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.gui.view;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.SortOrder;

import org.aergia.vinny.core.ApiResult;
import org.aergia.vinny.database.pojo.VinnyRole;
import org.aergia.vinny.database.pojo.VinnyUser;
import org.aergia.vinny.gui.controller.VinnyController;
import org.aergia.vinny.gui.types.ColumnDefinition;
import org.aergia.vinny.gui.util.DialogUtil;

public class UserPanel extends DatabasePojoPanel {

	private static class UserTableModel extends DatabasePojoTableModel<VinnyUser> {

		private static List<ColumnDefinition> definitions;

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		static {
			UserTableModel.definitions = new ArrayList<ColumnDefinition>();
			UserTableModel.definitions.add(new ColumnDefinition("Active", Boolean.class));
			UserTableModel.definitions.add(new ColumnDefinition("First Name", String.class, DatabasePojoTableModel.NAME_COLUMN_WIDTH, 1, SortOrder.ASCENDING));
			UserTableModel.definitions.add(new ColumnDefinition("Last Name", String.class, DatabasePojoTableModel.NAME_COLUMN_WIDTH, 0, SortOrder.ASCENDING));
			UserTableModel.definitions.add(new ColumnDefinition("User Name", String.class, DatabasePojoTableModel.NAME_COLUMN_WIDTH));
			UserTableModel.definitions.add(new ColumnDefinition("Modify Configuration", Boolean.class, 140));
			UserTableModel.definitions.add(new ColumnDefinition("Modify Service", Boolean.class, 125));
			UserTableModel.definitions.add(new ColumnDefinition("Modify User", Boolean.class, 125));
			UserTableModel.definitions.add(new ColumnDefinition("Modify Volunteer", Boolean.class, 125));
		}

		/**
		 * @param controller
		 */
		public UserTableModel(final VinnyController controller) {
			super(controller, UserTableModel.definitions);
		}

		@Override
		public Object getValueAt(final int rowIndex, final int columnIndex) {
			final VinnyUser user = this.records.get(rowIndex);
			switch (columnIndex) {
				case 0:
					return Boolean.valueOf(user.isActive());
				case 1:
					return user.getFirstName();
				case 2:
					return user.getLastName();
				case 3:
					return user.getUserName();
				case 4:
					return Boolean.valueOf(user.inRole(VinnyRole.MODIFY_CONFIGURATION));
				case 5:
					return Boolean.valueOf(user.inRole(VinnyRole.MODIFY_SERVICE));
				case 6:
					return Boolean.valueOf(user.inRole(VinnyRole.MODIFY_USER));
				case 7:
					return Boolean.valueOf(user.inRole(VinnyRole.MODIFY_VOLUNTEER));
				default:
					return null;
			}
		}

		@Override
		protected void populateTable() {
			this.records.addAll(this.controller.getUsers());
		}
	}

	private static final long serialVersionUID = 1L;

	public UserPanel(final VinnyController controller, final VinnyUser user) {
		super(controller, user, new UserTableModel(controller), true);
	}

	/**
	 * @param event
	 */
	@Override
	protected void addButtonActionPerformed(final ActionEvent event) {
		final UserEditor editor = new UserEditor(this.controller, this.user);
		final EditorDialog dialog = new EditorDialog(editor);

		if (dialog.isSaved()) {
			this.refresh();
		}
	}

	/**
	 * @param event
	 */
	@Override
	protected void deleteButtonActionPerformed(final ActionEvent event) {
		final int selectedRow = this.getSelectedRow();
		if (selectedRow == -1) {
			return;
		}

		final UserTableModel volunteerTableModel = (UserTableModel) this.tableModel;
		final VinnyUser disableUser = volunteerTableModel.getRecord(selectedRow);

		if (disableUser.getUserName().equals("admin")) {
			DialogUtil.alert(this, "You cannot disable the admin user.", "Disable user");
			return;
		}

		if (!DialogUtil.askQuestion(this, "Are you sure you want to disable " + disableUser.getName(), "Disable user")) {
			return;
		}

		disableUser.setActive(false);
		final ApiResult<VinnyUser> result = this.controller.saveUser(disableUser, this.user);
		if (!result.isError()) {
			this.refresh();
		} else {
			DialogUtil.alert(this, result.getText(), "Error");
		}
	}

	/**
	 * @param event
	 */
	@Override
	protected void editButtonActionPerformed(final ActionEvent event) {
		final int selectedRow = this.getSelectedRow();
		if (selectedRow != -1) {
			final UserTableModel volunteerTableModel = (UserTableModel) this.tableModel;
			final VinnyUser volunteer = volunteerTableModel.getRecord(selectedRow);
			final UserEditor editor = new UserEditor(this.controller, this.user, volunteer);
			final EditorDialog dialog = new EditorDialog(editor);
			if (dialog.isSaved()) {
				this.refresh();
			}
		}
	}

}
